/**
 * 首页（用户）
 */
package com.xiaomuwu.android.ui.home

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.xiaomuwu.android.R
import com.xiaomuwu.android.XiaoMuWuApplication
import com.xiaomuwu.android.ui.ForSearch
import kotlinx.android.synthetic.main.user_fragment.*

class UserFragment:Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.user_fragment,container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // 还原单选按钮选中状态
        when(ForSearch.userSortBy){
            "Res"->{
                radio_res.isChecked=true
            }
            "Fans"->{
                radio_fans.isChecked=true
            }
        }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        // 单选按钮选中监听
        userRadioGroup?.setOnCheckedChangeListener{_,checked->
            // 更新排序方式
            when(checked){
                R.id.radio_res->{
                    ForSearch.userSortBy="Res"  // “上传最多”
                }
                R.id.radio_fans->{
                    ForSearch.userSortBy="Fans" // “粉丝最多”
                }
            }

            updateUser()    // 刷新用户列表
        }
    }

    override fun onPause() {
        super.onPause()
        ForSearch.userSortBy = "Res"    // 重置用户排序方式
    }

    /**
     * 刷新用户列表
     */
    private fun updateUser(){
        val intent=Intent("com.xiaomuwu.android.UpdateUser")
        XiaoMuWuApplication.context.sendBroadcast(intent)
    }

}